/* Copyright (c) 2009 - 2010 98Labs Inc.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.sbal89.map.server;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import java.util.logging.Logger;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.gdata.data.spreadsheet.Cell;
import com.google.gdata.data.spreadsheet.CellEntry;
import com.google.gdata.util.ServiceException;
import com.sbal89.map.util.SDAUtil;

@SuppressWarnings("serial")
public class XmlServlet extends BaseServlet {
	private Logger log = Logger.getLogger(this.getClass().getName());
	
	@Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String type = request.getParameter("type");
		String key = request.getParameter("key");
		if (valid(type) && key != null) {
			String xml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";
			System.out.print("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n");
			int minRow = 2;
			int maxRow = 82;
			int minCol = 2;
			int maxCol = 0;
			if ("PR".equals(type) || "VP".equals(type)) {
				maxCol = 9;
			} else if ("SN".equals(type)) {
				maxCol = 59;
			}
			try {
				List<CellEntry> cells = SDAUtil.getCells(getSprService(), key, minRow, maxRow, minCol, maxCol);
				String rowString = "";
				int col = 0;
				int row = 0;
				xml += "<provinces>\n";
				System.out.print("<provinces>\n");
				for (int i=0; i<cells.size(); i++) {
					CellEntry cellEntry = cells.get(i);
					Cell cell = cellEntry.getCell();
					rowString += "C"+(col+1)+"=\"" + cell.getValue() +"\" ";
					col++;
					if ((i+1)%(maxCol-1)==0) {
						row++;
						xml += "\t<province name=\"P"+(row)+"\" " + rowString + "/>\n";
						System.out.print("\t<province name=\"P"+(row)+"\" " + rowString + "/>\n");
						rowString = "";
						col = 0;
					}
				}
				xml += "</provinces>\n";
				System.out.print("</provinces>\n");
				response.setContentType("text/xml");
				PrintWriter pw = response.getWriter();
				pw.print(xml);
				pw.flush();
				log.info("Generated "+type+" XML request for key=" + key);
			} catch (ServiceException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
    }
	
	private Boolean valid(String key) {
		boolean val = false;
		if (key != null) {
			key = key.toUpperCase();
			if ("PR".equals(key) || "VP".equals(key) || "SN".equals(key)) {
				val = true;
			}
		}
		return val;
	}
	
}
